import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import '../../../app_files.dart';
import 'dart:io';
import 'action.dart';
import 'state.dart';
import '../model/normal_config_model.dart';

Widget buildView(EngineerGasOpenViewState state, Dispatch dispatch,
    ViewService viewService) {

  Widget _infoWidget() => Container(
        padding: EdgeInsets.only(left: 15, right: 15, bottom: 8),
        decoration: BoxDecoration(
            color: Colors.white,
            border: Border(
                bottom: BorderSide(color: ThemeConfig.separeColor, width: 10))),
        child: Column(
          children: [
            Container(
              height: 44,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  '客户信息',
                  style: TextThemeConfig.Font16WithColorBold22,
                ),
              ),
              margin: EdgeInsets.only(bottom: 4),
            ),
            Container(
              padding: EdgeInsets.symmetric(vertical: 5),
              child: Row(
                children: [
                  Text(
                    '客户姓名',
                    style: TextThemeConfig.Font14WithColor66,
                  ),
                  Padding(padding: EdgeInsets.only(left: 50)),
                  Text(
                    state.model.userName,
                    style: TextThemeConfig.Font14WithColor22,
                  ),
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.symmetric(vertical: 5),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '客户地址',
                    style: TextThemeConfig.Font14WithColor66,
                  ),
                  Padding(padding: EdgeInsets.only(left: 50)),
                  Expanded(
                    child: Text(
                      state.model.address,
                      style: TextThemeConfig.Font14WithColor22,
                    ),
                  ),
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.symmetric(vertical: 5),
              child: Row(
                children: [
                  Text(
                    '联系方式',
                    style: TextThemeConfig.Font14WithColor66,
                  ),
                  Padding(padding: EdgeInsets.only(left: 50)),
                  Text(
                    state.model.phone,
                    style: TextThemeConfig.Font14WithColor22,
                  ),
                ],
              ),
            ),
            state.model.itemType == '6'
                ? Container(
                    padding: EdgeInsets.symmetric(vertical: 5),
                    child: Row(
                      children: [
                        Text(
                          '安装类型',
                          style: TextThemeConfig.Font14WithColor66,
                        ),
                        Padding(padding: EdgeInsets.only(left: 50)),
                        Text(
                          state.model.installTypeStr,
                          style: TextThemeConfig.Font14WithColor22,
                        ),
                        Expanded(
                          child: Container(),
                        ),
                        (int.tryParse(state.step) ?? 0) < 4 || state.step == '9'
                            ? Container()
                            : MyInkWellWidget(
                                child: Text(
                                  '修改类型',
                                  style: TextThemeConfig.Font14WithMainColor,
                                ),
                                onTap: () async {
                                  var res = await CommonUtils.pushRoute(
                                      viewService.context,
                                      RoutePath.installTypeEditView,
                                      arguments: {
                                        'type': state.model.installType,
                                        'itemID': state.model.itemID
                                      });
                                  if (res != null) {
                                    state.model.installType = res;
                                    dispatch(EngineerGasOpenViewActionCreator
                                        .onAction());
                                  }
                                },
                              )
                      ],
                    ),
                  )
                : Container(),
            (int.tryParse(state.step) ?? 0) < 4
                ? Container()
                : state.model.status == '9'
                    ? Container()
                    : Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            padding: EdgeInsets.symmetric(vertical: 5),
                            child: Row(
                              children: [
                                Text(
                                  '表具信息',
                                  style: TextThemeConfig.Font14WithColor66,
                                ),
                                Padding(padding: EdgeInsets.only(left: 50)),
                                state.meterInfoModel == null
                                    ? MyInkWellWidget(
                                        child: Container(
                                          width: 100,
                                          child: Text(
                                            '添加表具',
                                            style: TextThemeConfig
                                                .Font14WithMainColor,
                                          ),
                                        ),
                                        onTap: () async {
                                          var res = await CommonUtils.pushRoute(
                                              viewService.context,
                                              RoutePath.meterAddView,
                                              arguments: {
                                                'itemID': state.model.itemID
                                              });
                                          if (res != null) {
                                            state.meterInfoModel = res;
                                            dispatch(
                                                EngineerGasOpenViewActionCreator
                                                    .onAction());
                                          }
                                        },
                                      )
                                    : Expanded(
                                        child: Row(
                                          children: [
                                            Text(
                                              '${state.meterInfoModel.meterNo} / ${state.meterInfoModel.meterFactory}',
                                              style: TextThemeConfig
                                                  .Font14WithColor22,
                                            ),
                                            Expanded(child: Container()),
                                            MyInkWellWidget(
                                              child: Container(
                                                child: Text(
                                                  '删除',
                                                  style: TextThemeConfig
                                                      .Font14WithMainColor,
                                                ),
                                              ),
                                              onTap: () async {
                                                state.meterInfoModel = null;
                                                dispatch(
                                                    EngineerGasOpenViewActionCreator
                                                        .onAction());
                                              },
                                            )
                                          ],
                                        ),
                                      ),
                              ],
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.symmetric(vertical: 5),
                            child: Row(
                              children: [
                                Text(
                                  '材料列表',
                                  style: TextThemeConfig.Font14WithColor66,
                                ),
                                Padding(padding: EdgeInsets.only(left: 50)),
                                MyInkWellWidget(
                                  child: Container(
                                    width: 100,
                                    child: Text(
                                      state.materList.isEmpty ? '添加材料' : '查看材料',
                                      style:
                                          TextThemeConfig.Font14WithMainColor,
                                    ),
                                  ),
                                  onTap: () async {
                                    var res = await CommonUtils.pushRoute(
                                        viewService.context,
                                        RoutePath.engineerMaterAddView,
                                        arguments: {
                                          'isDetail': false,
                                          'list': state.materList,
                                          'type': state.model.rezGastype.isEmpty
                                              ? '1'
                                              : state.model.rezGastype,
                                        });
                                    dispatch(EngineerGasOpenViewActionCreator
                                        .onAction());
                                  },
                                )
                              ],
                            ),
                          ),
                        ],
                      ),
            state.logList.isEmpty ? Container() : Container(
              padding: EdgeInsets.symmetric(vertical: 5),
              child: Row(
                children: [
                  Text(
                    '首次派单时间',
                    style: TextThemeConfig.Font14WithColor66,
                  ),
                  Padding(padding: EdgeInsets.only(left: 25)),
                  Text(
                    '${state.logList.first['operatetime']}',
                    style: TextThemeConfig.Font14WithColor22,
                  ),
                ],
              ),
            )
          ],
        ),
      );

  Widget _bottomButton() {
    ///未接单
    if (state.step == '1') {
      return Container(
        margin: EdgeInsets.only(left: 15, right: 15, top: 30, bottom: 30),
        child: Row(
          children: [
            Expanded(
              child: Container(
                height: 45,
                decoration: BoxDecoration(
                  color: ThemeConfig.mainColor,
                  borderRadius: BorderRadius.all(Radius.circular(4)),
                ),
                child: InkWell(
                  child: Center(
                    child: Text(
                      '接单',
                      style: TextThemeConfig.Font16WithWhite,
                    ),
                  ),
                  onTap: () async {
                    CommonUtils.showWaitLoading(viewService.context);
                    ResultData res =
                        await CommonNetWork.fixGetOrder(state.model.itemID);
                    Navigator.pop(viewService.context);
                    if (res.success) {
                      CommonUtils.showToast(viewService.context, '接单成功');
                      bus.emit('refreshList');
                      dispatch(EngineerGasOpenViewActionCreator.onSetStep('2'));
                    } else {
                      CommonUtils.showErrorInfo(viewService.context, res.desc);
                    }
                  },
                ),
              ),
            ),
            Padding(padding: EdgeInsets.only(left: 24)),
            Expanded(
              child: Container(
                height: 45,
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(color: ThemeConfig.mainColor, width: 0.5),
                  borderRadius: BorderRadius.all(Radius.circular(4)),
                ),
                child: InkWell(
                  child: Center(
                    child: Text(
                      '转单',
                      style: TextThemeConfig.Font16WithMainColor,
                    ),
                  ),
                  onTap: () => CommonUtils.pushRoute(
                      viewService.context, RoutePath.orderTransferView,
                      arguments: {
                        'itemID': state.model.itemID,
                        'isEngineer': true,
                      }),
                ),
              ),
            ),
          ],
        ),
      );
    }

    if (state.step == '2') {
      return Column(
        children: [
          Container(
            margin: EdgeInsets.only(left: 15, right: 15, top: 30, bottom: 12),
            height: 45,
            decoration: BoxDecoration(
              color: ThemeConfig.mainColor,
              borderRadius: BorderRadius.all(Radius.circular(4)),
            ),
            child: InkWell(
              child: Center(
                child: Text(
                  '开始通气',
                  style: TextThemeConfig.Font16WithWhite,
                ),
              ),
              onTap: () async {
                int res = await CommonUtils.showOKCanceltView(
                    viewService.context, '确定开始通气？');
                if (res == 1) {
                  CommonUtils.showWaitLoading(viewService.context);
                  ResultData res =
                      await CommonNetWork.fixRepairOrder(state.model.itemID);
                  Navigator.pop(viewService.context);
                  if (res.success) {
                    CommonUtils.showToast(viewService.context, '工单已为处理中状态');
                    dispatch(EngineerGasOpenViewActionCreator.onSetStep('4'));
                    bus.emit('refreshList');
                  } else {
                    CommonUtils.showErrorInfo(viewService.context, res.desc);
                  }
                }
              },
            ),
          ),
          Container(
            margin: EdgeInsets.only(left: 15, right: 15, bottom: 30),
            height: 45,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(4)),
            ),
            child: InkWell(
              child: Center(
                child: Text(
                  '其他情况',
                  style: TextThemeConfig.Font13WithMainColor,
                ),
              ),
              onTap: () async {
                CommonUtils.pushRoute(
                    viewService.context, RoutePath.engineerIndoorReasonView,
                    arguments: {'item': state.model});
              },
            ),
          ),
        ],
      );
    }

    if (state.step == '4') {
      return Container(
        margin: EdgeInsets.only(left: 15, right: 15, top: 30, bottom: 30),
        height: 45,
        decoration: BoxDecoration(
          color: ThemeConfig.mainColor,
          borderRadius: BorderRadius.all(Radius.circular(4)),
        ),
        child: InkWell(
          child: Center(
            child: Text(
              '下一步',
              style: TextThemeConfig.Font16WithWhite,
            ),
          ),
          onTap: () => dispatch(EngineerGasOpenViewActionCreator.onSubmit()),
        ),
      );
    }

    if (state.step == '9') {
      return Column(
        children: [
          Container(
            margin: EdgeInsets.only(left: 15, right: 15, top: 30, bottom: 30),
            height: 45,
            decoration: BoxDecoration(
              color: ThemeConfig.mainColor,
              borderRadius: BorderRadius.all(Radius.circular(4)),
            ),
            child: InkWell(
              child: Center(
                child: Text(
                  '重新安装',
                  style: TextThemeConfig.Font16WithWhite,
                ),
              ),
              onTap: () async {
                CommonUtils.showWaitLoading(viewService.context);
                ResultData res =
                await CommonNetWork.engineerOrderReset(state.model.itemID);
                Navigator.pop(viewService.context);
                if (res.success) {
                  bus.emit('refreshList');
                  dispatch(EngineerGasOpenViewActionCreator.onSetStep('4'));
                } else {
                  CommonUtils.showErrorInfo(viewService.context, res.desc);
                }
              },
            ),
          ),

          Container(
            height: 45,
            width: 120,
            child: MyInkWellWidget(
              child: Center(
                child: Text(
                  '转单',
                  style: TextThemeConfig.Font16WithMainColor,
                ),
              ),
              onTap: () => CommonUtils.pushRoute(
                  viewService.context, RoutePath.orderTransferView,
                  arguments: {
                    'itemID': state.model.itemID,
                    'isEngineer': true,
                  }),
            ),
          ),
        ],
      );
    }
    return Container();
  }

  Widget _fixInfoContent() {
    String imgInfo =
        CommonUtils.checkNullToString(state.model.businessExtend['imgInfo']);
    List images = [];
    if (imgInfo.isNotEmpty) {
      images = imgInfo.split(',');
    }
    return Container(
      child: Column(
        children: [
          Container(
            padding: EdgeInsets.only(left: 15, right: 15, bottom: 15),
            decoration: BoxDecoration(
                color: Colors.white,
                border: Border(
                    bottom:
                        BorderSide(color: ThemeConfig.separeColor, width: 10))),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  height: 44,
                  child: Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '隐患整改',
                      style: TextThemeConfig.Font16WithColorBold22,
                    ),
                  ),
                ),
                Container(
                  height: 100,
                  child: Text(
                    state.remark,
                    style: TextThemeConfig.Font13WithColor22,
                  ),
                ),
              ],
            ),
          ),
          images.isEmpty
              ? Container()
              : Container(
                  padding: EdgeInsets.only(left: 15, right: 15),
                  decoration: BoxDecoration(
                      color: Colors.white,
                      border: Border(
                          bottom: BorderSide(
                              color: ThemeConfig.separeColor, width: 10))),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        height: 44,
                        child: Align(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            '拍照留存',
                            style: TextThemeConfig.Font16WithColorBold22,
                          ),
                        ),
                        margin: EdgeInsets.only(bottom: 4),
                      ),
                      Container(
                        margin: EdgeInsets.only(bottom: 12),
                        height: 90,
                        child: ListView.builder(
                          itemBuilder: (BuildContext context, int index) {
                            var file = images[index];
                            return InkWell(
                              child: Container(
                                margin: EdgeInsets.only(right: 12),
                                child: file is File
                                    ? Image(
                                        image: FileImageEx(file),
                                        width: 90,
                                        height: 90,
                                        fit: BoxFit.fill,
                                      )
                                    : CustomImage(
                                        file,
                                        fit: BoxFit.fill,
                                        width: 90,
                                        height: 90,
                                      ),
                              ),
                              onTap: () {
                                CommonUtils.showMediaAlbumView(
                                  viewService.context,
                                  initialIndex: state.orderPhoto.indexOf(file),
                                  files: images,
                                );
                              },
                            );
                          },
                          itemCount: images.length,
                          scrollDirection: Axis.horizontal,
                          itemExtent: 112,
                          padding: EdgeInsets.only(),
                        ),
                      )
                    ],
                  ),
                )
        ],
      ),
    );
  }

  Widget _repairWidget() {
    List<Widget> list = state.items == null
        ? []
        : state.items.map<Widget>((e) {
            return NormalConfigWidget(
                e,
                (type, model, {dynamic file}) => dispatch(
                    EngineerGasOpenViewActionCreator.onTap(type, model, file)));
          }).toList();

    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: list,
      ),
    );
  }

  return Scaffold(
    appBar: myAppBar(viewService.context, title: '点火通气工单'),
    body: SafeArea(
      child: GestureDetector(
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _infoWidget(),
              state.step == '4'
                  ? _repairWidget()
                  : state.step == '9'
                      ? _fixInfoContent()
                      : Container(),
              _bottomButton(),
            ],
          ),
          controller: state.scrollController,
        ),
        onTap: () => CommonUtils.closeKey(viewService.context),
        behavior: HitTestBehavior.translucent,
      ),
    ),
  );
}
